 //控制层 
app.controller('goodsController' ,function($scope,$controller,$location,itemCatService ,goodsService){
	
	$controller('baseController',{$scope:$scope});//继承
	
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		goodsService.findAll().success(
			function(response){
				$scope.list=response.list;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		goodsService.findPage(page,rows).success(
			function(response){
				$scope.list=response.list;
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}

//查询实体
    $scope.findOne=function(){
        var id= $location.search()['id']; //获取页面上传过来的参数 id;search()为$location服务自带的方法,获得所有参数封装为数组

        if (id == null) { //如果没有参数,直接返回
            return;
        }
        goodsService.findOne(id).success(
            function(response){
                $scope.entity= response;

                //向富文本编辑器中添加商品介绍内容
                editor.html($scope.entity.goodsDesc.introduction);

                //显示商品图片
                if ($scope.entity.goodsDesc.itemImages != null){
                    $scope.entity.goodsDesc.itemImages=JSON.parse($scope.entity.goodsDesc.itemImages);
                } else {
                    $scope.entity.goodsDesc.itemImages=[];
                }

                //显示扩展属性
                $scope.entity.goodsDesc.customAttributeItems=JSON.parse($scope.entity.goodsDesc.customAttributeItems);

                //规格
                $scope.entity.goodsDesc.specificationItems=JSON.parse($scope.entity.goodsDesc.specificationItems);

                //SKU列表规格列转换
                for (var i = 0; i < $scope.entity.itemList.length; i++) {
                    $scope.entity.itemList[i].spec=JSON.parse($scope.entity.itemList[i].spec);
                }
            }
        );
    }
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.entity.id!=null){//如果有ID
			serviceObject=goodsService.update( $scope.entity ); //修改  
		}else{
			serviceObject=goodsService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.success){
					//重新查询 
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}
	
	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框
		if (confirm("是否确认删除？")) {
            goodsService.dele( $scope.selectIds ).success(
                function(response){
                    if(response.success){
                        $scope.reloadList();//刷新列表
                        $scope.selectIds=[];
                    }
                }
            );
		}
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		goodsService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.list;
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}

    $scope.status=['未审核','审核通过','审核未通过','审核中'];//商品状态
    $scope.itemCatList=[];//商品分类列表
    //查询商品分类
    $scope.findItemCatList=function(){
        itemCatService.findAll().success(
            function(response){
                for(var i=0;i<response.length;i++){
                    $scope.itemCatList[response[i].id ]=response[i].name;
                }
            }
        );
    }

    //批量修改商品审核状态
	$scope.updateStatus=function (status) {
		goodsService.updateStatus($scope.selectIds,status).success(
			function (response) {
				if (response.success) {
					$scope.reloadList();//刷新列表,清空id集合
					$scope.selectIds=[];
				}else {
					alert(response.message)
				}
            }
		)
    }




    //读取一级分类
    $scope.selectItemCat1List=function () {
        itemCatService.findByParentId(0).success(
            function (response) {
                $scope.itemCat1List=response;
            }
        )
    }

    //读取二级分类
    $scope.$watch('entity.goods.category1Id',function (newValue,oldValue) { //当变量 'entity.goods.category1Id' 变化后出发function()函数
        //根据选择的值,查询二级分类
        itemCatService.findByParentId(newValue).success(
            function (response) {
                $scope.itemCat2List=response;
            }
        )
    })

    //读取三级分类
    $scope.$watch('entity.goods.category2Id',function (newValue,oldValue) {
        //根据选择的值,查询二级分类
        itemCatService.findByParentId(newValue).success(
            function (response) {
                $scope.itemCat3List=response;
            }
        )
    })


    //根据规格名称和规格选项名称返回是否被勾选
    $scope.checkAttributeValue=function (specName,optionName) {
        var items=$scope.entity.goodsDesc.specificationItems; //一个具体商品的规格集合

        var object=$scope.searchObjectByKey(items,'attributeName',specName);//从集合中按照key(attributeName)查询对象

        if (object != null) {
            if (object.attributeValue.indexOf(optionName) >= 0) { //如果能查询到规格选项
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }
});	
